import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
//ExpansionTiles可用于生成两级或多级列表。
//这个应用程序用ExpansionTiles显示分层数据。点击一个标题会展开或折叠其子视图。
class Entry{
  Entry(this.title, [this.children = const <Entry>[]]);
  final String title;
  final List<Entry> children;
}

final List<Entry> data = <Entry>[
  Entry('Chapter A',
  <Entry>[
    Entry('Section A0',
    <Entry>[
      Entry('Item A0.1'),
      Entry('Item A0.2'),
      Entry('Item A0.3'),
    ]),
    Entry('Section A1'),
    Entry('Section A2'),
  ]),
  Entry('Chapter B',
  <Entry>[
    Entry('Section B0'),
    Entry('Section B1'),
  ]),
  Entry('Chapter C',
  <Entry>[
    Entry('Section C0'),
    Entry('Section C1'),
    Entry('Section C2',
    <Entry>[
      Entry('Item C2.0'),
      Entry('Item C2.1'),
      Entry('Item C2.2'),
      Entry('Item C2.3'),
    ])
  ]),
];

class EntryItem extends StatelessWidget{
  const EntryItem(this.entry);
  final Entry entry;

  Widget _buildTiles(Entry root){
    if(root.children.isEmpty){
      return ListTile(title: Text(root.title),);
    }else{
      return ExpansionTile(title: Text(root.title),
      key: PageStorageKey<Entry>(root),
      children: root.children.map(_buildTiles).toList(),);
    }
  }
  @override
  Widget build(BuildContext context) {
    return _buildTiles(entry);
  }
}
class ExpansionTileEx extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('ExpansionTile'),
      ),
      body: ListView.builder(itemBuilder: (BuildContext context, int index){
        return EntryItem(data[index]);
      },
      itemCount: data.length,),
    );
  }
}